package com.lzp.controller;

import com.lzp.pojo.SysUser;
import com.lzp.service.UserService;
import com.lzp.utils.JSONResult;
import com.lzp.utils.PagedGridResult;
import org.apache.commons.lang3.StringUtils;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * @description: 测试CRUD操作
 * @author: lzp
 * @createTime: 2021-08-04 00:04
 **/
@RestController
@RequestMapping("mybatis")
public class MybatisCRUDController {

    @Autowired
    private UserService userService;

    @Autowired
    private Sid sid;

    @RequestMapping("/saveUser")
    public JSONResult saveUser() {
        SysUser user = new SysUser();
        user.setId(sid.nextShort());
        user.setUsername("lzp" + new Date());
        user.setNickname("lzp" + new Date());
        user.setPassword("123456");
        user.setIsDelete(0);
        user.setRegistTime(new Date());

        userService.saveUser(user);

        return JSONResult.ok("保存成功");
    }

    @GetMapping("updateUser")
    public JSONResult updateUser() {
        SysUser user = new SysUser();
        user.setId("21080404Y4T2S8ZC");
        user.setUsername("xxx" + new Date());
        user.setNickname("xxx" + new Date());

        userService.updateUser(user);

        return JSONResult.ok("更新成功");
    }

    @GetMapping("deleteUser")
    public JSONResult deleteUser(String userId) {
        userService.deleteUser(userId);

        return JSONResult.ok("删除成功");
    }

    @RequestMapping("/queryUserById")
    public JSONResult queryUserById(String userId) {
        return JSONResult.ok(userService.queryUserById(userId));
    }

    @RequestMapping("/queryUserList")
    public JSONResult queryUserList() {
        SysUser user = new SysUser();
        user.setUsername("lzp");

        return JSONResult.ok(userService.queryUserList(user));
    }

    @RequestMapping("/queryUserListPaged")
    public JSONResult queryUserListPaged(Integer page) {
        if (page == null) {
            page = 1;
        }
        int pageSize = 10;

        SysUser user = new SysUser();
        user.setUsername("lzp");

        List<SysUser> userList = userService.queryUserListPaged(user, page, pageSize);

        return JSONResult.ok(userList);
    }

    @RequestMapping("/queryUserListPagedGrid")
    public JSONResult queryUserListPagedGrid(
            @RequestParam String username,
            @RequestParam Integer page,
            @RequestParam Integer pageSize) {
        if (page == null) {
            page = 1;
        }
        SysUser user = new SysUser();
        user.setUsername(username);

        PagedGridResult pagedGridResult = userService.queryUserListPagedGrid(user, page, pageSize);

        return JSONResult.ok(pagedGridResult);
    }

    @RequestMapping("queryUserByIdCustom")
    public JSONResult queryUserByIdCustom(String id) {
        if (StringUtils.isBlank(id)) {
            return JSONResult.errorMsg("用户id为空");
        }
        SysUser sysUser = userService.queryUserByIdCustom(id);
        return JSONResult.ok(sysUser);
    }


    @RequestMapping("/saveUserTransactional")
    public JSONResult saveUserTransactional() {
        SysUser user = new SysUser();
        user.setId(sid.nextShort());
        user.setPassword("123");
        user.setUsername("transactional");
        user.setNickname("transactional");
        user.setIsDelete(0);
        user.setRegistTime(new Date());

        userService.saveUserTransactional(user);

        return JSONResult.ok(user);
    }
}
